package com.example.library.studentfragment;

import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.library.R;
import com.example.library.adapter.ReservationHistoryAdapter;
import com.example.library.model.ReservationHistory;
import com.example.library.util.LocalStorageManager;

import java.util.List;

public class StudentReservationFragment extends Fragment {
    private RecyclerView recyclerView;
    private ReservationHistoryAdapter adapter;
    private LocalStorageManager storageManager;
    private Button btnClearAll;
    private String currentUserId = "current_user_id"; // 这里需要替换为实际的用户ID

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.student_reservation, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 初始化本地存储管理器
        storageManager = new LocalStorageManager(requireContext());

        // 初始化RecyclerView
        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));

        // 初始化清空按钮
        btnClearAll = view.findViewById(R.id.btnClearAll);
        btnClearAll.setOnClickListener(v -> showClearAllConfirmDialog());

        // 获取当前用户的预约历史
        loadReservationHistory();
    }

    @Override
    public void onResume() {
        super.onResume();
        // 每次显示Fragment时刷新预约历史
        loadReservationHistory();
    }

    private void loadReservationHistory() {
        List<ReservationHistory> historyList = storageManager.getReservationHistoryForUser(currentUserId);
        if (adapter == null) {
            adapter = new ReservationHistoryAdapter(requireContext(), historyList);
            recyclerView.setAdapter(adapter);
        } else {
            adapter.updateData(historyList);
        }
        
        // 根据是否有历史记录来显示或隐藏清空按钮
        btnClearAll.setVisibility(historyList.isEmpty() ? View.GONE : View.VISIBLE);
    }

    private void showClearAllConfirmDialog() {
        new AlertDialog.Builder(requireContext())
                .setTitle("清空确认")
                .setMessage("确定要清空所有预约历史记录吗？")
                .setPositiveButton("确定", (dialog, which) -> {
                    // 清空当前用户的所有预约历史
                    storageManager.deleteAllReservationHistoryForUser(currentUserId);
                    // 刷新列表
                    loadReservationHistory();
                })
                .setNegativeButton("取消", null)
                .show();
    }
} 